﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using MillerApp.UserControls.Enums;


namespace MillerApp.UserControls
{
    ///   <summary> 
    ///   PublicFrm   的摘要说明。 
    ///   </summary> 
    public class EnitityFormBase : FormBase
    {
        ///   <summary> 
        ///   必需的设计器变量。 
        ///   </summary> 
        private System.ComponentModel.Container components = null;

        public EnitityFormBase()
        {
            // 
            //   Windows   窗体设计器支持所必需的 
            // 
            InitializeComponent();

            // 
            //   TODO:   在   InitializeComponent   调用后添加任何构造函数代码 
            // 
        }

        ///   <summary> 
        ///   清理所有正在使用的资源。 
        ///   </summary> 
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region   Windows   窗体设计器生成的代码
        public UserControls.DataMappingContainer dataMappingContainer;
        private ActionType actionType = ActionType.AddNew;
        /// <summary>
        /// 操作类型
        /// </summary>
        public ActionType ActionTypeCode
        {
            get { return actionType; }
            set { actionType = value; }
        }

        ///   <summary> 
        ///   设计器支持所需的方法   -   不要使用代码编辑器修改 
        ///   此方法的内容。 
        ///   </summary> 
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            dataMappingContainer = new MillerApp.UserControls.DataMappingContainer(this.components);
        }
        #endregion

        private MillerApp.CommonLib.Utility.ReflectionHelper reflectionHelper = new CommonLib.Utility.ReflectionHelper();

        /// <summary>
        /// 实体映射到控件
        /// </summary>
        /// <param name="entity"></param>
        protected void Mapping2Control(ref object entity)
        {
            if (entity == null)
            {
                return;
            }
            this.GetControl(this, ref entity, true);
        }
        /// <summary>
        /// 控件映射到实体
        /// </summary>
        /// <param name="entity"></param>
        protected void Mapping2Entity(ref object entity)
        {
            if (entity == null)
            {
                return;
            }
            this.GetControl(this, ref entity, false);
        }

        /// <summary>
        /// 递归遍历所有控件
        /// </summary>
        /// <param name="ctrls"></param>
        private void GetControl(Control ctrls,ref object entity,bool isEntity2Control)
        {
            foreach (Control c in ctrls.Controls)
            {
                if (c.Controls.Count > 0)
                {
                    GetControl(c,ref entity,isEntity2Control);
                }
                else
                {
                    string p=this.dataMappingContainer.GetMappingProperty(c);//映射的实体的属性名称
                    if (string.IsNullOrEmpty(p))
                    {
                        continue;
                    }
                    string p2 = this.dataMappingContainer.GetMappingControlProperty(c);//映射的控件属性名称
                    if (string.IsNullOrEmpty(p2))
                    {
                        p2 = "Text";
                    }
                    if (isEntity2Control == false)
                    {
                        object n = reflectionHelper.GetPropertyValue(c, p2);
                        reflectionHelper.SetPropertyValue(entity, p, n);
                    }
                    else
                    {
                        object n = reflectionHelper.GetPropertyValue(entity, p);
                        reflectionHelper.SetPropertyValue(c, p2, n);
                    }
                }
            }
        }
    }
}